package io.leo.leoutils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    private static String[] weekendList = {"日","一","二","三","四","五","六"};
    public static String getStrOfCurrentTime(Date olderDate){
        long time = olderDate.getTime();
        Date nowDate = new Date();
        long nowTime = nowDate.getTime();
        long length = nowTime-time;
        long sec = length/1000;
        if (sec<60)return sec+"秒前";
        long min = sec/60;
        if (min<60)return min+"分钟前";
        long hour = min/60;
        if (hour<6)return hour+"小时前";
        if (isThisDay(olderDate)) {
            if (hour < 24) return new SimpleDateFormat("HH:mm").format(olderDate);
        }else if (daysBetween(olderDate,nowDate)==1)
            return "昨天";
        else if (daysBetween(olderDate,nowDate)==2)
            return "前天";
        if (isThisWeek(olderDate))return "星期"+getWeekendStr(olderDate);
        if (isThisMonth(olderDate))return new SimpleDateFormat("dd号").format(olderDate);
        if (isThisYear(olderDate))return new SimpleDateFormat("MM月dd号").format(olderDate);
        else return new SimpleDateFormat("yyyy年MM月dd号").format(olderDate);
    }

    public static boolean isThisYear(Date time){
        return isThisTime(time,"yyyy");
    }
    public static boolean isThisMonth(Date time) {
        return isThisTime(time, "yyyy-MM");
    }
    public static boolean isThisDay(Date time){
        return isThisTime(time,"yyyy-MM-dd");
    }
    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate,Date bdate)  {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            smdate=sdf.parse(sdf.format(smdate));
            bdate=sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            long between_days=(time2-time1)/(1000*3600*24);

            return Integer.parseInt(String.valueOf(between_days));
        } catch (ParseException e) {
            throw new RuntimeException("ParseException"+e.getMessage()+e.getStackTrace());
        }
    }


    public static boolean isThisTime(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    public static int getWeekend(Date time){
        Calendar c=Calendar.getInstance();
        c.setTime(time);
        return c.get(Calendar.DAY_OF_WEEK);
    }

    public static String getWeekendStr(Date time){
        int weekend = getWeekend(time);
        return weekendList[weekend];
    }
    //判断选择的日期是否是本周
    public static boolean isThisWeek(Date time) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(time);
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if (paramWeek == currentWeek) {
            return true;
        }
        return false;
    }
}
